package org.bluedream.core.module.controller.bill.stkIn;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.comm.utils.RestResult;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.entity.bill.stkIn.StkIn;
import org.bluedream.core.module.entity.bill.stkIn.dtl.StkInDtl;
import org.bluedream.core.module.entity.goods.Goods;
import org.bluedream.core.module.service.bill.stkIn.StkInService;
import org.bluedream.core.module.service.bill.stkIn.dtl.StkInDtlService;
import org.bluedream.core.module.service.goods.GoodsService;
import org.bluedream.core.utils.DictUtil;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @ClassName StkInController       采购入库
 * @Description TODO
 * @Author foxsand
 * @Data 2021-11-19 9:03
 * @Version
 */
@Slf4j
@Controller
@RequestMapping(value = "${adminPath}/stkIn")
public class StkInController extends BaseController {
    @Resource
    private StkInService stkInService;

    @Resource
    private StkInDtlService stkInDtlService;

    @Resource
    private GoodsService goodsService;

    @ModelAttribute
    public StkIn get(String id){
        return stkInService.getById(id);
    }

    @RequiresPermissions("stkIn:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(StkIn entity, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("stkIn" , entity);
        return "module/bill/stkIn/stkInList";
    }

    @RequiresPermissions("stkIn:view")
    @RequestMapping("/form")
    public String form(StkIn entity, Model model){
        entity.setBillType(DictUtil.getDictValue("sys.bill.type", "采购入库单", ""));
        model.addAttribute("stkIn" , entity);
        return "module/bill/stkIn/stkInForm";
    }

/*    @RequiresPermissions("stkIn:view")
    @RequestMapping("/form4Dtl")
    public String form4Dtl(StkIn entity, Model model){
        model.addAttribute("stkIn" , entity);
        return "module/bill/stkIn/stkInDtlForm";
    }*/

    @RequiresPermissions("stkIn:view")
    @RequestMapping(value = "/goodsForm")
    public String goodsForm(Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("goods" , new Goods());
        return "module/goods/goodsSelect";
    }

    @RequiresPermissions("stkIn:edit")
    @RequestMapping("/getGoods")
    @ResponseBody
    public RestResult getGoods(Goods entity){
        return goodsService.getOne(entity);
    }

    @RequiresPermissions("stkIn:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<StkIn> findPage(@RsaSecurityParameter StkIn entity){
        return stkInService.findPage(entity);
    }

    @RequiresPermissions("stkIn:view")
    @RequestMapping("/findPage4Dtl")
    @ResponseBody
    public IPage<StkInDtl> findPage4Dtl(@RsaSecurityParameter StkIn entity){
        return stkInDtlService.findPage(entity);
    }

    @RequiresPermissions("stkIn:edit")
    @RequestMapping("/save")
    @ResponseBody
    public RestResult save(StkIn entity){
        return stkInService.saveEntity(entity);
    }

    @RequiresPermissions("stkIn:check")
    @RequestMapping("/check")
    @ResponseBody
    public RestResult check(StkIn entity){
        return stkInService.check(entity);
    }

    /**
     * 导入excel演示
     * @param file
     */
    @RequiresPermissions("goods:view")
    @RequestMapping(value = "/excelImport")
    @ResponseBody
    public IPage<StkInDtl> excelImport(@RequestParam("file") MultipartFile file){
        return stkInService.excelImport(file);
    }

}
